import Vue from 'vue'
import { getToken } from '@/utils/storage'

const BASE_URL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'
const request = {}
const header = {
    'source-client': 'miniapp',
    Authorization: getToken()
}

function returnPromise(url, data, method) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + url,
            data,
            method,
            header,
            success: res => {
                resolve(res.data)
            },
            fail: err => {
                reject(err)
            }
        })
    })
}

request.get = (url, data) => {
    const method = 'GET'
    return returnPromise(url, data, method)
}

request.post = (url, data) => {
    const method = 'POST'
    return returnPromise(url, data, method)
}

request.put = (url, data) => {
    const method = 'PUT'
    return returnPromise(url, data, method)
}

request.delete = (url, data) => {
    const method = 'DELETE'
    return returnPromise(url, data, method)
}

Vue.prototype.request = request

export default request
